using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HydraShellServer.Services;
using Hydra.Shell.Interfaces;
#if SILVERLIGHT
using System.Windows.Threading;
using System.ServiceModel.DomainServices.Client;
using HydraQueryProvider.QueryProviders;
using System.Windows.Browser;
using Utils;
using HydraShellServer.Implementations;
using Hydra.Shell.EntityPaths;
#endif
using System.Diagnostics;
using System.Collections.Specialized;
using Hydra.Shell.QueryProviders;
using System.Xml.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Xml;

namespace Hydra.Shell.Services
{
    public class HydraSynchronizerService : IHydraSynchronizerService
    {
#if SILVERLIGHT
        private DispatcherTimer synchronizationTimer;
        private bool synchronizationStarted;
        private PathBuilder pathBuilder;

        public HydraSynchronizerService(IHydraShell shell, ISynchronousProxy synchronousProxy)
        {
            pathBuilder = new PathBuilder(shell, synchronousProxy);
        }

        public void LoadServerService(Guid guidService, IUnknown service, string type)
        {
            if (!synchronizationStarted)
            {
                StartSynchronization();
            }

            pathBuilder.RegisterRoot(service, guidService);
        }

        public void LoadServerPackage(IHydraPackage package, string type)
        {
            if (!synchronizationStarted)
            {
                StartSynchronization();
            }

            pathBuilder.RegisterRoot(package);
        }

        private void StartSynchronization()
        {
            var waiting = false;

            synchronizationTimer = new DispatcherTimer();

            synchronizationStarted = true;

            synchronizationTimer.Tick += (sender2, e2) =>
            {
                if (!waiting) // Monitor.TryEnter not working
                {
                    waiting = true;

                    try
                    {
                        //var query = context.GetUpdatesQuery();
                        //var operation = context.Load<SynchronizationUpdate>(query);

                        //operation.Completed += (sender, e) =>
                        //{
                        //    try
                        //    {
                        //        foreach (var packageUpdate in operation.Entities)
                        //        {

                        //        }
                        //    }
                        //    catch (Exception ex)
                        //    {
                        //        Debugger.Break();
                        //    }

                        //    waiting = false;
                        //};
                    }
                    catch (Exception ex)
                    {
                        Debugger.Break();
                    }
                }
            };

            synchronizationTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000); // one second
            synchronizationTimer.Start();
        }

#endif
    }
}
